package com.njsoft.bodyawakening.view;

import android.annotation.SuppressLint;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;
import android.widget.RelativeLayout;

import com.njsoft.bodyawakening.R;
import com.njsoft.bodyawakening.utils.APPInfo;
import com.njsoft.bodyawakening.utils.TimeUtil;

import java.util.List;

public class GridRelativeLayout extends RelativeLayout {
    private Canvas myCanvas;
    private int horNum = 8, verNum = 8;//水平网格和竖直网格
    private int screenW, screenH;//屏幕宽和高
    private boolean initOver = false;//初始化标签
    private int verGrid;

    public GridRelativeLayout(Context context) {
        super(context);
        setWillNotDraw(false);
    }

    public GridRelativeLayout(Context context, AttributeSet attrs) {
        super(context, attrs);
        setWillNotDraw(false);
    }

    public GridRelativeLayout(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        setWillNotDraw(false);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        this.myCanvas = canvas;
        Paint paint = new Paint();
        paint.setColor(getResources().getColor(R.color.GridViewDivide));//颜色
        paint.setStrokeWidth(3);//线宽
        // 纵线间距(宽度/纵向条数)
        verGrid = (int) (screenW / (verNum - 1));
        //水平线间距(长度/水平线多少)
        int horGrid = verGrid;
        if (initOver) {
            //画横线
            for (int i = 0; i < horNum; i++) {
                canvas.drawLine(0, i * verGrid, screenW, i * verGrid,
                        paint);
            }
            //画纵线
            for (int i = 0; i < verNum; i++) {
                canvas.drawLine(i * horGrid, 0, i * horGrid, screenH,
                        paint);
            }
        }

        //TODO 计算纵坐标

        float minY = getTimeY(TimeUtil.getTimeHHmm());
        paint.setColor(getResources().getColor(R.color.red_normal));//颜色
        paint.setStrokeWidth(3);//线宽
        //绘制当前时间轴
        canvas.drawLine(0, minY, screenW, minY,
                paint);
        paint.setTextSize(APPInfo.dp2px(getContext(), 18));


    }

    /**
     * 设置网格线参数
     **/
    public void setInf(int verNum, int horNum, int screenW, int screenH) {
        this.verNum = verNum;
        this.horNum = horNum;
        this.screenW = screenW;
        this.screenH = screenH;
        initOver = true;
        postInvalidate();
    }

    /**
     * 擦除网格线
     **/
    public void clearLine() {
        initOver = false;
        postInvalidate();
    }

    public interface MyItemClickListener {
        void onClickPoint(String time);
    }

    public void setClickListener(MyItemClickListener clickListener) {
        this.clickListener = clickListener;

    }

    private MyItemClickListener clickListener;

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        int x = (int) event.getX();
        int y = (int) event.getY();
        int action = event.getAction();
        switch (action) {
            case MotionEvent.ACTION_DOWN:
                break;
            case MotionEvent.ACTION_MOVE:
                break;
            case MotionEvent.ACTION_UP:
                int harfVerGrid = verGrid / 2;
                int yNum = y / harfVerGrid;
                int yLast = y % harfVerGrid;
                if (yLast != 0) {
                    yNum = yNum + 1;
                }
                int xNum = x / verGrid;
                int xLast = x % verGrid;
                if (xLast != 0) {
                    xNum = xNum + 1;
                }
                clickListener.onClickPoint("坐标 x:" + x + " y：" + y + "\n 第" + yNum + "行" + " 第" + xNum + "列" + TimeUtil.getTimeHHmm());
                break;
        }
        return true;
    }

    /**
     * 添加矩形区域
     *
     * @param startTime
     * @param endTime
     */
    public void addTimeView(String startTime, String endTime) {


        Paint paint = new Paint();
        paint.setColor(getResources().getColor(R.color.red_normal));//颜色
        paint.setStrokeWidth(3);//线宽
        myCanvas.drawRect(getTimeX(true, startTime), getTimeY(startTime), getTimeX(false, endTime), getTimeY(endTime), paint);
    }

    /**
     * 计算某时间的纵坐标
     *
     * @param time
     * @return
     */
    private float getTimeY(String time) {
        List<String> min = TimeUtil.getListFormATimetoBtime(getResources().getString(R.string.BeginTime), time, 60 * 1000);
        float minDes = verGrid / (float) 60;
        //TODO 计算纵坐标
        float minY = minDes * (min.size() - 1);
        return minY;
    }

    private float getTimeX(boolean isBegin, String time) {
        float x = 0;
        int week = TimeUtil.getWeek(time);
        if (isBegin) {
            x = verGrid * (week - 1);
        } else {
            x = verGrid * week;
        }
        return x;
    }
}